package com.ahao.aggregation.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.InputType;
import android.widget.EditText;
import android.widget.LinearLayout;

public class CustomLoginDialog {

    public interface LoginResultListener {
        void onLoginSuccess(String username, String password);

        void onLoginFailure();
    }

    public static void showLoginDialog(Context context, final LoginResultListener listener) {
        // 创建一个线性布局
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.VERTICAL);

        // 创建账号输入框
        final EditText usernameEditText = new EditText(context);
        usernameEditText.setHint("请输入账号");
        layout.addView(usernameEditText);

        // 创建密码输入框
        final EditText passwordEditText = new EditText(context);
        passwordEditText.setHint("请输入密码");
        passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        layout.addView(passwordEditText);

        // 创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("登录")
                .setView(layout)
                .setPositiveButton("成功", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String username = usernameEditText.getText().toString();
                        String password = passwordEditText.getText().toString();
                        listener.onLoginSuccess(username, password);
                    }
                })
                .setNegativeButton("失败", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onLoginFailure();
                    }
                })
                .setCancelable(false);  // 不可点击对话框外部取消

        // 显示对话框
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
